package com.gomyb.redis;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author <a href="mailto:HelloHeSir@gmail.com">Mr_He</a>
 */
@Getter
@Setter
@Component
@Configuration
@ConfigurationProperties(prefix = "redis")
public class RedisConfig {

    private String host = "127.0.0.1";

    private String password = "";

    private Integer port = 6379;

    private Integer timeout = 5000;

    @Bean
    public JedisPool redisPoolFactory() {
        GenericObjectPoolConfig config = new JedisPoolConfig();
        JedisPool JedisPool;
        if (null != password && !"".equals(password)) {
            JedisPool = new JedisPool(config, host, port, timeout, password);
        } else {
            JedisPool = new JedisPool(config, host, port, timeout);
        }
        try (Jedis Jedis = JedisPool.getResource()) {
            return JedisPool;
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new RuntimeException("jedis connect failed...");
    }

}